#include "main/inc/sys_utils.h"
#include "main/inc/sys_fun.h"
#include "main/inc/goods.h"

// LED相关
#define TEST_MAGIC 'x' // 定义幻数
#define LED1 _IO(TEST_MAGIC, 0)
#define LED2 _IO(TEST_MAGIC, 1)
#define LED3 _IO(TEST_MAGIC, 2)
#define LED4 _IO(TEST_MAGIC, 3)
#define LED_ON 0  // 灯亮
#define LED_OFF 1 // 灯灭
int led_fd;       // LED文件描述符

typedef lv_obj_t *Page, *Button, *Label, *Textarea, *MessageBox, *Keyboard, *Image;

Page vend_page, login_page, manage_page;     // 购买界面、登陆界面、管理界面
Page add_page, del_page, upd_page, qry_page; // 增删改查页

/*
    一级购买界面组件
*/
Button goto_login_btn, coin_return_btn;
Label vend_title_lb, goto_login_btn_lb, coin_return_btn_lb, balance_label;
float balance = 0;                         // 余额
int key_fd;                                // 按键设备文件描述符
char key[4];                               // 存储按键状态
const int key_amounts[4] = {10, 5, 1, 20}; // 按键值对应的金额
struct good * head;
// 商品相关全局变量
int current_page      = 0;                 // 当前页码
int selected_index    = -1;                // 当前选中的商品索引
int purchase_quantity = 1;                 // 购买数量
Label goods_labels[5];                     // 商品标签数组
Button prev_page_btn, next_page_btn;       // 翻页按钮
Button quantity_inc_btn, quantity_dec_btn; // 数量加减按钮
Label quantity_label;                      // 数量显示标签
Button purchase_btn;                       // 购买按钮
Image goods_images[5];                     // 商品图片
/*
    二级登陆界面组件
*/
Button login_rt_btn, goto_manage_btn;
Label login_title_lb, login_rt_btn_lb, goto_manage_btn_lb;
Textarea name_ta, passwd_ta;
Keyboard login_kb;
lv_obj_t * pinyin_ime; // 拼音输入法对象
lv_obj_t * cand_panel; // 候选词面板
/*
    三级管理界面组件
*/
Button manage_rt_btn, manage_add_btn, manage_del_btn, manage_upd_btn, manage_qry_btn;
Label manage_rt_btn_lb, manage_add_btn_lb, manage_del_btn_lb, manage_upd_btn_lb, manage_qry_btn_lb;
/*
    四级增删改查界面组件
*/
// 增
Button add_rt_btn, add_btn;
Label add_rt_btn_lb, add_title_lb;
Textarea add_name_ta, add_num_ta, add_price_ta, add_path_ta;
Keyboard add_kb;
// 删
Button del_rt_btn, del_btn;
Label del_rt_btn_lb, del_title_lb;
Textarea del_name_ta;
Keyboard del_kb;
// 改
Button upd_rt_btn, upd_btn, upd_query_btn;
Label upd_rt_btn_lb, upd_title_lb;
Textarea upd_name_ta, upd_num_ta, upd_price_ta;
Keyboard upd_kb;
// 查
Button qry_rt_btn, qry_btn;
Label qry_rt_btn_lb, qry_title_lb;
Textarea qry_name_ta;
Keyboard qry_kb;

static lv_style_t style;  // 字体样式
static lv_ft_info_t info; // 字体信息
static lv_style_t style_bg;

// 字体初始化
void font_init();
/*
    一级购买界面
*/
void goto_vend();
void coins_return(lv_event_t * e);          // 退钱事件
void key_read_callback(lv_timer_t * timer); // 按键读取回调函数（使用LVGL定时器）
// 商品相关函数声明
void update_goods_display();
void select_good(lv_event_t * e);
void prev_page(lv_event_t * e);
void next_page(lv_event_t * e);
void increase_quantity(lv_event_t * e);
void decrease_quantity(lv_event_t * e);
void purchase_good(lv_event_t * e);
/*
    二级登陆界面
*/
void goto_login(lv_event_t * e);
void name_ta_cb(lv_event_t * e);
void passwd_ta_cb(lv_event_t * e);
static void ta_event_cb(lv_event_t * e);
// 从登录界面返回购买界面
void login_to_vend(lv_event_t * e);
/*
    三级管理界面
*/
void goto_manage(lv_event_t * e);
void create_manage();
// 验证登录信息
int check_login();
// 从管理界面返回购买界面
void manage_to_vend(lv_event_t * e);
/*
    四级增删改查界面
*/
// 增
void goto_add(lv_event_t * e);       // 上架商品响应函数
void add_to_manage(lv_event_t * e);  // 从新增界面返回管理界面
void add_good_click(lv_event_t * e); // 上架商品处理函数
// 删
void goto_del(lv_event_t * e);          // 下架商品响应函数
void del_to_manage(lv_event_t * e);     // 从删除界面返回管理界面
void delete_good_click(lv_event_t * e); // 删除商品处理函数
// 改
void goto_upd(lv_event_t * e);              // 修改商品响应函数
void upd_to_manage(lv_event_t * e);         // 从修改界面返回管理界面
void update_good_click(lv_event_t * e);     // 执行商品修改操作
void query_and_fill_update(lv_event_t * e); // 查询商品并填充到修改表单
// 查
void goto_qry(lv_event_t * e);         // 查询商品响应函数
void qry_to_manage(lv_event_t * e);    // 从查询界面返回管理界面
void query_good_click(lv_event_t * e); // 查询商品处理函数

int main(void)
{
    lvgl_fbdev_init();

    printf("LVGL v%d.%d.%d, program starting...\n", lv_version_major(), lv_version_minor(), lv_version_patch());

    head = init_list();
    insert_tail(head, "辣条", 10, 5.50);
    insert_tail(head, "可乐", 20, 3.00);
    insert_tail(head, "雪碧", 15, 3.50);
    insert_tail(head, "薯片", 12, 4.00);
    insert_tail(head, "矿泉水", 30, 2.00);
    insert_tail(head, "爆米花", 8, 6.00);
    insert_tail(head, "巧克力", 18, 8.50);
    insert_tail(head, "果汁", 25, 4.50);
    set_path(head, "辣条", "S:/test_prj/imgs/latiao.jpg");
    set_path(head, "可乐", "S:/test_prj/imgs/kele.jpg");
    set_path(head, "雪碧", "S:/test_prj/imgs/xuebi.jpg");
    set_path(head, "薯片", "S:/test_prj/imgs/shupian.jpg");
    set_path(head, "矿泉水", "S:/test_prj/imgs/kuangquanshui.jpg");
    set_path(head, "爆米花", "S:/test_prj/imgs/baomihua.jpg");
    set_path(head, "巧克力", "S:/test_prj/imgs/qiaokeli.jpg");
    set_path(head, "果汁", "S:/test_prj/imgs/guozhi.jpg");

    // 打开LED
    led_fd = open("/dev/Led", O_RDWR);
    if(led_fd < 0) {
        perror("open led failed");
        return -1;
    }
    ioctl(led_fd, LED1, LED_OFF);
    ioctl(led_fd, LED2, LED_OFF);
    ioctl(led_fd, LED3, LED_OFF);
    ioctl(led_fd, LED4, LED_OFF);

    // 打开按键设备
    key_fd = open("/dev/key_drv", O_RDWR);
    if(key_fd < 0) {
        perror("open key failed");
        return -1;
    }
    lv_timer_create(key_read_callback, 20, NULL); // 创建LVGL定时器用于读取按键，每隔20ms读取一次按键状态

    font_init();
    goto_vend();

    lv_task_loop();

    // 清理资源
    free_list(head);
    close(led_fd);
    close(key_fd);
    return 0;
}

// 按键读取回调函数（使用LVGL定时器）
void key_read_callback(lv_timer_t * timer)
{
    char last_key[4] = {0}; // 记录上一次按键状态，用于检测按键边缘
    int ret          = read(key_fd, key, sizeof(key));

    if(ret == 4) {
        // 检测按键按下边沿（从释放到按下）
        for(int i = 0; i < 4; i++) {
            if(key[i] == 1 && last_key[i] == 0) {
                // 修改余额
                balance += key_amounts[i];
                char buf[32];
                sprintf(buf, "当前余额: %.2f元", balance);
                lv_label_set_text(balance_label, buf);
                // 创建投币提示
                char msg[64];
                sprintf(msg, "投币 %d 元", key_amounts[i]);
                MessageBox coin_msgbox = lv_msgbox_create(lv_scr_act(), "投币成功", msg, NULL, true);
                lv_obj_set_pos(coin_msgbox, 280, 320);
                Label coin_msg_text = lv_msgbox_get_text(coin_msgbox);
                lv_obj_add_style(coin_msg_text, &style, 0);
                Label coin_msg_title = lv_msgbox_get_title(coin_msgbox);
                lv_obj_add_style(coin_msg_title, &style, 0);
                // 打印到控制台
                // printf("投币 %d 元, 当前余额: %.2f元\n", key_amounts[i], balance);

                // LED反馈
                switch(i) {
                    printf("led\n");
                    case 2:
                        ioctl(led_fd, LED1, LED_ON);
                        usleep(500000);
                        ioctl(led_fd, LED1, LED_OFF);
                        break;
                    case 1:
                        ioctl(led_fd, LED2, LED_ON);
                        usleep(500000);
                        ioctl(led_fd, LED2, LED_OFF);
                        break;
                    case 0:
                        ioctl(led_fd, LED3, LED_ON);
                        usleep(500000);
                        ioctl(led_fd, LED3, LED_OFF);
                        break;
                    case 3:
                        ioctl(led_fd, LED4, LED_ON);
                        usleep(500000);
                        ioctl(led_fd, LED4, LED_OFF);
                        break;
                    default: break;
                }
                usleep(500000); // 简单消抖
            }
            last_key[i] = key[i];
        }
    }
}

void font_init()
{
    // 初始化字体
    info.name   = "/font/simhei.ttf";
    info.weight = 24;
    info.style  = FT_FONT_STYLE_NORMAL;
    info.mem    = NULL;
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }

    // 用info的font创建style
    lv_style_init(&style);
    lv_style_set_text_font(&style, info.font);
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_AUTO);

    lv_style_init(&style_bg);
    lv_style_set_bg_color(&style_bg, lv_color_hex(0xE0F7FA)); // 背景颜色
    lv_style_set_bg_opa(&style_bg, LV_OPA_50);                // 背景的透明度
    lv_style_set_border_color(&style_bg, lv_color_white());   // 边框的颜色
}

// 一级购买界面
void goto_vend()
{
    // 创建一级贩卖界面目录
    vend_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(vend_page, 800, 480);
    lv_obj_set_pos(vend_page, 0, 0);
    lv_obj_add_style(vend_page, &style_bg, 0);

    // 创建标题
    vend_title_lb = lv_label_create(vend_page);
    lv_obj_set_size(vend_title_lb, 600, 80);        // 大小
    lv_obj_set_pos(vend_title_lb, 90, 10);          // 坐标
    lv_obj_add_style(vend_title_lb, &style, 0);     // 中文样式
    lv_label_set_text(vend_title_lb, "贩卖机界面"); // 添加文本

    // 显示当前余额
    balance_label = lv_label_create(vend_page);
    lv_obj_set_size(balance_label, 300, 50);
    lv_obj_set_pos(balance_label, 50, 50);
    lv_obj_add_style(balance_label, &style, 0);
    // 修改余额
    char buf[32];
    sprintf(buf, "当前余额: %.2f元", balance);
    lv_label_set_text(balance_label, buf);

    // 退币按钮
    coin_return_btn = lv_btn_create(vend_page);
    lv_obj_set_size(coin_return_btn, 100, 50); // 大小
    lv_obj_set_pos(coin_return_btn, 400, 25);  // 坐标
    // 设置按钮文本
    coin_return_btn_lb = lv_label_create(coin_return_btn);
    lv_obj_add_style(coin_return_btn_lb, &style, 0);                            // 中文样式
    lv_label_set_text(coin_return_btn_lb, "退币");                              // 添加文本
    lv_obj_add_event_cb(coin_return_btn, coins_return, LV_EVENT_CLICKED, NULL); // 添加响应函数

    // 进入二级登陆界面按钮
    goto_login_btn = lv_btn_create(vend_page);
    lv_obj_set_size(goto_login_btn, 100, 50); // 大小
    lv_obj_set_pos(goto_login_btn, 600, 25);  // 坐标
    // 设置按钮文本
    goto_login_btn_lb = lv_label_create(goto_login_btn);
    lv_obj_add_style(goto_login_btn_lb, &style, 0);                          // 中文样式
    lv_label_set_text(goto_login_btn_lb, "管理");                            // 添加文本
    lv_obj_add_event_cb(goto_login_btn, goto_login, LV_EVENT_CLICKED, NULL); // 添加响应函数

    // 创建商品显示标签 - 调整为一行5个
    for(int i = 0; i < 5; i++) {
        // 图片信息
        goods_images[i] = lv_img_create(vend_page);
        lv_obj_set_size(goods_images[i], 120, 160);
        lv_obj_set_pos(goods_images[i], (i * 150) + 10, 100);

        goods_labels[i] = lv_btn_create(vend_page);    // 使用按钮而不是标签，方便点击
        lv_obj_set_size(goods_labels[i], 140, 90);     // 调整大小以适应一行5个
        lv_obj_set_pos(goods_labels[i], i * 150, 270); // 调整位置
        lv_obj_add_style(goods_labels[i], &style, 0);
        lv_obj_add_event_cb(goods_labels[i], select_good, LV_EVENT_CLICKED, NULL);

        // 在按钮内部创建标签用于显示文本
        Label btn_label = lv_label_create(goods_labels[i]);
        lv_label_set_text(btn_label, ""); // 初始为空
        lv_obj_center(btn_label);
        lv_obj_add_style(btn_label, &style, 0);
        lv_obj_set_user_data(goods_labels[i], btn_label); // 将标签存储为用户数据
    }

    // 创建翻页和购买控制区域
    prev_page_btn = lv_btn_create(vend_page);
    lv_obj_set_size(prev_page_btn, 110, 50);
    lv_obj_set_pos(prev_page_btn, 50, 380);
    Label prev_page_label = lv_label_create(prev_page_btn);
    lv_obj_add_style(prev_page_label, &style, 0);
    lv_label_set_text(prev_page_label, "上一页");
    lv_obj_add_event_cb(prev_page_btn, prev_page, LV_EVENT_CLICKED, NULL);

    quantity_dec_btn = lv_btn_create(vend_page);
    lv_obj_set_size(quantity_dec_btn, 50, 50);
    lv_obj_set_pos(quantity_dec_btn, 200, 380);
    Label dec_label = lv_label_create(quantity_dec_btn);
    lv_obj_add_style(dec_label, &style, 0);
    lv_label_set_text(dec_label, "-");
    lv_obj_add_event_cb(quantity_dec_btn, decrease_quantity, LV_EVENT_CLICKED, NULL);

    quantity_label = lv_label_create(vend_page);
    lv_obj_set_size(quantity_label, 60, 40);
    lv_obj_set_pos(quantity_label, 300, 390);
    lv_obj_add_style(quantity_label, &style, 0);
    lv_label_set_text(quantity_label, "1");

    quantity_inc_btn = lv_btn_create(vend_page);
    lv_obj_set_size(quantity_inc_btn, 50, 50);
    lv_obj_set_pos(quantity_inc_btn, 360, 380);
    Label inc_label = lv_label_create(quantity_inc_btn);
    lv_obj_add_style(inc_label, &style, 0);
    lv_label_set_text(inc_label, "+");
    lv_obj_add_event_cb(quantity_inc_btn, increase_quantity, LV_EVENT_CLICKED, NULL);

    purchase_btn = lv_btn_create(vend_page);
    lv_obj_set_size(purchase_btn, 100, 50);
    lv_obj_set_pos(purchase_btn, 450, 380);
    Label purchase_label = lv_label_create(purchase_btn);
    lv_obj_add_style(purchase_label, &style, 0);
    lv_label_set_text(purchase_label, "购买");
    lv_obj_add_event_cb(purchase_btn, purchase_good, LV_EVENT_CLICKED, NULL);

    next_page_btn = lv_btn_create(vend_page);
    lv_obj_set_size(next_page_btn, 110, 50);
    lv_obj_set_pos(next_page_btn, 600, 380);
    Label next_page_label = lv_label_create(next_page_btn);
    lv_obj_add_style(next_page_label, &style, 0);
    lv_label_set_text(next_page_label, "下一页");
    lv_obj_add_event_cb(next_page_btn, next_page, LV_EVENT_CLICKED, NULL);

    // 更新商品显示
    update_goods_display();
}

// 退钱事件
void coins_return(lv_event_t * e)
{
    if(balance > 0) {
        // 修改余额
        balance = 0;
        lv_label_set_text(balance_label, "当前余额: 0.00元");
        // 创建退币提示
        MessageBox coin_msgbox = lv_msgbox_create(lv_scr_act(), "退币成功", "当前余额为0", NULL, true);
        lv_obj_set_pos(coin_msgbox, 280, 320);
        Label coin_msg_text = lv_msgbox_get_text(coin_msgbox);
        lv_obj_add_style(coin_msg_text, &style, 0);
        Label coin_msg_title = lv_msgbox_get_title(coin_msgbox);
        lv_obj_add_style(coin_msg_title, &style, 0);

        // LED反馈
        ioctl(led_fd, LED1, LED_ON);
        ioctl(led_fd, LED2, LED_ON);
        ioctl(led_fd, LED3, LED_ON);
        ioctl(led_fd, LED4, LED_ON);
        usleep(500000);
        ioctl(led_fd, LED1, LED_OFF);
        ioctl(led_fd, LED2, LED_OFF);
        ioctl(led_fd, LED3, LED_OFF);
        ioctl(led_fd, LED4, LED_OFF);
        usleep(500000);
    } else {
        MessageBox coin_msgbox = lv_msgbox_create(lv_scr_act(), "退币失败", "当前余额为0", NULL, true);
        lv_obj_set_pos(coin_msgbox, 280, 320);
        Label coin_msg_text = lv_msgbox_get_text(coin_msgbox);
        lv_obj_add_style(coin_msg_text, &style, 0);
        Label coin_msg_title = lv_msgbox_get_title(coin_msgbox);
        lv_obj_add_style(coin_msg_title, &style, 0);
    }
}

// 更新商品显示
void update_goods_display()
{
    struct good * current = head->next;
    int total_goods       = 0;

    // 计算商品总数
    while(current != head) {
        total_goods++;
        current = current->next;
    }

    // 计算总页数
    int total_pages = (total_goods + 4) / 5;
    if(total_pages == 0) total_pages = 1;

    // 确保当前页在有效范围内
    if(current_page >= total_pages) current_page = total_pages - 1;
    if(current_page < 0) current_page = 0;

    // 重置选中状态
    selected_index    = -1;
    purchase_quantity = 1;
    lv_label_set_text(quantity_label, "1");

    // 重置所有商品信息样式
    for(int i = 0; i < 5; i++) {
        lv_obj_set_style_bg_color(goods_labels[i], lv_color_hex(0x555555), 0);
        lv_img_set_src(goods_images[i], "");
    }

    // 显示当前页的商品
    current         = head->next;
    int start_index = current_page * 5;

    // 移动到当前页的第一个商品
    for(int i = 0; i < start_index && current != head; i++) {
        current = current->next;
    }

    // 显示商品信息
    for(int i = 0; i < 5 && current != head; i++) {
        Label btn_label = (Label)lv_obj_get_user_data(goods_labels[i]);
        char buf[64];
        snprintf(buf, sizeof(buf), "%s\n%.2f元\n库存:%d", current->name, current->price, current->num);
        lv_label_set_text(btn_label, buf);

        // 显示商品图片
        lv_img_set_src(goods_images[i], current->path);
        current = current->next;
    }

    // 清空剩余的商品按钮
    for(int i = (total_goods - current_page * 5); i < 5 && i >= 0; i++) {
        Label btn_label = (Label)lv_obj_get_user_data(goods_labels[i]);
        lv_label_set_text(btn_label, "");
    }
}

// 选择商品
void select_good(lv_event_t * e)
{
    Label * target = lv_event_get_target(e);

    // 找出点击的是哪个商品按钮
    for(int i = 0; i < 5; i++) {
        if(goods_labels[i] == target) {
            // 计算商品在链表中的实际索引
            int actual_index      = current_page * 5 + i;
            struct good * current = head->next;

            // 检查这个索引是否有效
            for(int j = 0; j < actual_index && current != head; j++) {
                current = current->next;
            }

            if(current != head && current->num > 0) {
                // 更新选中状态
                selected_index = actual_index;

                // 高亮显示选中的商品
                for(int j = 0; j < 5; j++) {
                    if(j == i) {
                        lv_obj_set_style_bg_color(goods_labels[j], lv_color_hex(0xFFA500), 0);
                    } else {
                        lv_obj_set_style_bg_color(goods_labels[j], lv_color_hex(0x555555), 0);
                    }
                }

                // 重置购买数量为1
                purchase_quantity = 1;
                lv_label_set_text(quantity_label, "1");
            }
            break;
        }
    }
}

// 上一页
void prev_page(lv_event_t * e)
{
    if(current_page > 0) {
        current_page--;
        update_goods_display();
    }
}

// 下一页
void next_page(lv_event_t * e)
{
    struct good * current = head->next;
    int total_goods       = 0;

    // 计算商品总数
    while(current != head) {
        total_goods++;
        current = current->next;
    }

    // 计算总页数
    int total_pages = (total_goods + 4) / 5;
    if(total_pages == 0) total_pages = 1;

    if(current_page < total_pages - 1) {
        current_page++;
        update_goods_display();
    }
}

// 增加购买数量
void increase_quantity(lv_event_t * e)
{
    if(selected_index >= 0) {
        // 获取选中的商品
        struct good * current = head->next;
        for(int i = 0; i < selected_index && current != head; i++) {
            current = current->next;
        }

        if(current != head && purchase_quantity < current->num) {
            purchase_quantity++;
            char buf[10];
            snprintf(buf, sizeof(buf), "%d", purchase_quantity);
            lv_label_set_text(quantity_label, buf);
        }
    }
}

// 减少购买数量
void decrease_quantity(lv_event_t * e)
{
    if(selected_index >= 0 && purchase_quantity > 1) {
        purchase_quantity--;
        char buf[10];
        snprintf(buf, sizeof(buf), "%d", purchase_quantity);
        lv_label_set_text(quantity_label, buf);
    }
}

// 购买商品
void purchase_good(lv_event_t * e)
{
    if(selected_index < 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "请先选择商品", NULL, true);
        lv_obj_set_pos(msgbox, 280, 320);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 获取选中的商品
    struct good * current = head->next;
    for(int i = 0; i < selected_index && current != head; i++) {
        current = current->next;
    }

    if(current == head) {
        return;
    }

    // 检查库存
    if(current->num < purchase_quantity) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "库存不足", NULL, true);
        lv_obj_set_pos(msgbox, 280, 320);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 检查余额
    float total_price = current->price * purchase_quantity;
    if(balance < total_price) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "余额不足", NULL, true);
        lv_obj_set_pos(msgbox, 280, 320);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 执行购买
    current->num -= purchase_quantity;
    balance -= total_price;
    save_goods_to_file(head);

    // 更新余额显示
    char buf[32];
    sprintf(buf, "当前余额: %.2f元", balance);
    lv_label_set_text(balance_label, buf);

    // 显示购买成功消息
    char msg[64];
    snprintf(msg, sizeof(msg), "成功购买 %d 个 %s", purchase_quantity, current->name);
    MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "购买成功", msg, NULL, true);
    lv_obj_set_pos(msgbox, 280, 320);
    Label msg_text = lv_msgbox_get_text(msgbox);
    lv_obj_add_style(msg_text, &style, 0);
    Label msg_title = lv_msgbox_get_title(msgbox);
    lv_obj_add_style(msg_title, &style, 0);

    // 更新商品显示
    update_goods_display();

    // LED反馈
    ioctl(led_fd, LED1, LED_ON);
    ioctl(led_fd, LED2, LED_ON);
    ioctl(led_fd, LED3, LED_ON);
    ioctl(led_fd, LED4, LED_ON);
    usleep(500000);
    ioctl(led_fd, LED1, LED_OFF);
    ioctl(led_fd, LED2, LED_OFF);
    ioctl(led_fd, LED3, LED_OFF);
    ioctl(led_fd, LED4, LED_OFF);
}

// 二级登陆界面
void goto_login(lv_event_t * e)
{
    lv_obj_del(vend_page); // 关闭购买界面

    // 创建二级登陆界面
    login_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(login_page, 800, 480);

    // 创建返回按钮
    login_rt_btn = lv_btn_create(login_page);
    lv_obj_set_size(login_rt_btn, 100, 50);
    lv_obj_set_pos(login_rt_btn, 0, 25);
    login_rt_btn_lb = lv_label_create(login_rt_btn);
    lv_obj_add_style(login_rt_btn_lb, &style, 0);
    lv_label_set_text(login_rt_btn_lb, "返回");
    lv_obj_add_event_cb(login_rt_btn, login_to_vend, LV_EVENT_CLICKED, NULL); // 添加返回事件

    // 创建标题
    login_title_lb = lv_label_create(login_page);
    lv_obj_set_size(login_title_lb, 300, 80);            // 大小
    lv_obj_set_pos(login_title_lb, 200, 10);             // 坐标
    lv_obj_add_style(login_title_lb, &style, 0);         // 中文样式
    lv_label_set_text(login_title_lb, "管理员登陆界面"); // 添加文本

    // 创建名称输入框
    name_ta = lv_textarea_create(login_page);
    lv_obj_set_size(name_ta, 500, 80);
    lv_obj_set_pos(name_ta, 120, 60);
    lv_textarea_set_one_line(name_ta, true); // 设置只能显示一行
    lv_obj_add_style(name_ta, &style, 0);    // 让用户名输入使用中文字体样式
    lv_textarea_set_text(name_ta, "gec");
    lv_textarea_set_placeholder_text(name_ta, "请输入用户名"); // 给用户名输入框设置默认提示信息

    // 创建密码输入框
    passwd_ta = lv_textarea_create(login_page);
    lv_obj_set_size(passwd_ta, 500, 80);
    lv_obj_set_pos(passwd_ta, 120, 150);
    lv_textarea_set_one_line(passwd_ta, true);        // 设置只能显示一行
    lv_textarea_set_password_mode(passwd_ta, true);   // 设置隐藏密码
    lv_textarea_set_password_show_time(passwd_ta, 3); // 设置3ms之后马上隐藏密码
    lv_textarea_set_max_length(passwd_ta, 8);         // 设置密码最多只能是8位
    lv_obj_add_style(passwd_ta, &style, 0);           // 让密码输入使用中文字体样式
    lv_textarea_set_text(passwd_ta, "123456");
    lv_textarea_set_placeholder_text(passwd_ta, "请输入不超过8位密码"); // 给密码输入框设置默认提示信息

    // 创建登陆按钮
    goto_manage_btn = lv_btn_create(login_page);
    lv_obj_set_size(goto_manage_btn, 100, 50);
    lv_obj_set_pos(goto_manage_btn, 650, 25);
    Label goto_manage_btn_lb = lv_label_create(goto_manage_btn);
    lv_obj_add_style(goto_manage_btn_lb, &style, 0);
    lv_label_set_text(goto_manage_btn_lb, "登录");
    lv_obj_add_event_cb(goto_manage_btn, goto_manage, LV_EVENT_CLICKED, NULL); // 添加登陆事件

    // 创建拼音输入法
    pinyin_ime = lv_ime_pinyin_create(login_page);
    lv_obj_set_style_text_font(pinyin_ime, info.font, 0); // 使用已初始化的字体
    // 创建键盘并关联到拼音输入法
    login_kb = lv_keyboard_create(login_page);
    lv_obj_set_size(login_kb, 800, 170);
    lv_ime_pinyin_set_keyboard(pinyin_ime, login_kb);
    // 获取候选词面板并设置位置和大小
    cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
    lv_obj_set_size(cand_panel, LV_PCT(100), 30);
    lv_obj_align_to(cand_panel, login_kb, LV_ALIGN_OUT_TOP_MID, 0, 0);
    lv_obj_add_style(cand_panel, &style, 0);
    // 给文本区添加事件回调
    lv_obj_add_event_cb(name_ta, ta_event_cb, LV_EVENT_ALL, login_kb);
    lv_obj_add_event_cb(passwd_ta, ta_event_cb, LV_EVENT_ALL, login_kb);
    // 默认将键盘与用户名输入框关联
    lv_keyboard_set_textarea(login_kb, name_ta);
}

void name_ta_cb(lv_event_t * e)
{
    // 把软键盘跟用户名输入框关联
    lv_keyboard_set_textarea(login_kb, name_ta);
}
void passwd_ta_cb(lv_event_t * e)
{
    // 把软键盘跟密码输入框关联
    lv_keyboard_set_textarea(login_kb, passwd_ta);
}

// 返回购买界面
void login_to_vend(lv_event_t * e)
{
    lv_obj_del(login_page); // 把登陆界面关闭
    goto_vend();            // 返回购买界面
}

// 拼音输入
static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    Textarea * ta        = lv_event_get_target(e);
    Keyboard * kb        = lv_event_get_user_data(e);

    if(code == LV_EVENT_FOCUSED) {
        if(lv_indev_get_type(lv_indev_get_act()) != LV_INDEV_TYPE_KEYPAD) {
            lv_keyboard_set_textarea(kb, ta);
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(cand_panel, LV_OBJ_FLAG_HIDDEN); // 显示候选词面板
        }
    } else if(code == LV_EVENT_CANCEL) {
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(cand_panel, LV_OBJ_FLAG_HIDDEN); // 隐藏候选词面板
        lv_obj_clear_state(ta, LV_STATE_FOCUSED);
        lv_indev_reset(NULL, ta);
    }
}

// 三级管理界面
void goto_manage(lv_event_t * e)
{
    if(check_login()) {
        lv_obj_del(login_page); // 关闭登陆界面
        create_manage();
    }
}

void create_manage()
{
    // 创建三级管理界面
    manage_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(manage_page, 800, 480);

    // 创建返回按钮
    manage_rt_btn = lv_btn_create(manage_page);
    lv_obj_set_size(manage_rt_btn, 100, 50);
    lv_obj_set_pos(manage_rt_btn, 0, 25);
    manage_rt_btn_lb = lv_label_create(manage_rt_btn);
    lv_obj_add_style(manage_rt_btn_lb, &style, 0);
    lv_label_set_text(manage_rt_btn_lb, "注销");
    lv_obj_add_event_cb(manage_rt_btn, manage_to_vend, LV_EVENT_CLICKED, NULL); // 添加返回事件

    // 增
    manage_add_btn = lv_btn_create(manage_page);
    lv_obj_set_size(manage_add_btn, 200, 50);
    lv_obj_set_pos(manage_add_btn, 150, 150);
    manage_add_btn_lb = lv_label_create(manage_add_btn);
    lv_obj_add_style(manage_add_btn_lb, &style, 0);
    lv_label_set_text(manage_add_btn_lb, "上架商品");
    lv_obj_add_event_cb(manage_add_btn, goto_add, LV_EVENT_CLICKED, NULL); // 添加返回事件

    // 删
    manage_del_btn = lv_btn_create(manage_page);
    lv_obj_set_size(manage_del_btn, 200, 50);
    lv_obj_set_pos(manage_del_btn, 450, 150);
    manage_del_btn_lb = lv_label_create(manage_del_btn);
    lv_obj_add_style(manage_del_btn_lb, &style, 0);
    lv_label_set_text(manage_del_btn_lb, "下架商品");
    lv_obj_add_event_cb(manage_del_btn, goto_del, LV_EVENT_CLICKED, NULL); // 添加返回事件

    // 改
    manage_upd_btn = lv_btn_create(manage_page);
    lv_obj_set_size(manage_upd_btn, 200, 50);
    lv_obj_set_pos(manage_upd_btn, 150, 350);
    manage_upd_btn_lb = lv_label_create(manage_upd_btn);
    lv_obj_add_style(manage_upd_btn_lb, &style, 0);
    lv_label_set_text(manage_upd_btn_lb, "修改商品");
    lv_obj_add_event_cb(manage_upd_btn, goto_upd, LV_EVENT_CLICKED, NULL); // 添加返回事件

    // 查
    manage_qry_btn = lv_btn_create(manage_page);
    lv_obj_set_size(manage_qry_btn, 200, 50);
    lv_obj_set_pos(manage_qry_btn, 450, 350);
    manage_qry_btn_lb = lv_label_create(manage_qry_btn);
    lv_obj_add_style(manage_qry_btn_lb, &style, 0);
    lv_label_set_text(manage_qry_btn_lb, "查询商品");
    lv_obj_add_event_cb(manage_qry_btn, goto_qry, LV_EVENT_CLICKED, NULL); // 添加返回事件
}

int check_login()
{
    const char * name   = lv_textarea_get_text(name_ta);
    const char * passwd = lv_textarea_get_text(passwd_ta);
    // 判断用户名和密码
    if(strcmp(name, "gec") == 0 && strcmp(passwd, "123456") == 0) {
        return 1;
    } else {
        // 弹出消息盒子
        MessageBox err_msgbox = lv_msgbox_create(lv_scr_act(), "提示", "账号或密码错误", NULL, true);
        lv_obj_set_pos(err_msgbox, 280, 120);
        Label err_msg_text = lv_msgbox_get_text(err_msgbox);
        lv_obj_add_style(err_msg_text, &style, 0);
        Label err_msg_title = lv_msgbox_get_title(err_msgbox);
        lv_obj_add_style(err_msg_title, &style, 0);

        return 0;
    }
}

// 返回购买界面
void manage_to_vend(lv_event_t * e)
{
    lv_obj_del(manage_page); // 把管理界面关闭
    goto_vend();
}

// 上架商品界面
void goto_add(lv_event_t * e)
{
    lv_obj_del(manage_page); // 把管理界面关闭

    // 创建上架界面
    add_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(add_page, 800, 480);

    // 添加返回按钮
    add_rt_btn = lv_btn_create(add_page);
    lv_obj_set_size(add_rt_btn, 100, 50);
    lv_obj_set_pos(add_rt_btn, 0, 25);
    add_rt_btn_lb = lv_label_create(add_rt_btn);
    lv_obj_add_style(add_rt_btn_lb, &style, 0);
    lv_label_set_text(add_rt_btn_lb, "返回");
    lv_obj_add_event_cb(add_rt_btn, add_to_manage, LV_EVENT_CLICKED, NULL);

    // 创建标题
    add_title_lb = lv_label_create(add_page);
    lv_obj_set_size(add_title_lb, 300, 80);          // 大小
    lv_obj_set_pos(add_title_lb, 200, 10);           // 坐标
    lv_obj_add_style(add_title_lb, &style, 0);       // 中文样式
    lv_label_set_text(add_title_lb, "上架商品界面"); // 添加文本

    // Textarea
    add_name_ta = lv_textarea_create(add_page);
    lv_obj_set_size(add_name_ta, 240, 50);
    lv_obj_set_pos(add_name_ta, 120, 40);
    lv_textarea_set_one_line(add_name_ta, true); // 设置只能显示一行
    lv_obj_add_style(add_name_ta, &style, 0);    // 让商品名输入使用中文字体样式
    lv_textarea_set_text(add_name_ta, "");
    lv_textarea_set_placeholder_text(add_name_ta, "请输入商品名"); // 给商品名输入框设置默认提示信息

    add_num_ta = lv_textarea_create(add_page);
    lv_obj_set_size(add_num_ta, 240, 50);
    lv_obj_set_pos(add_num_ta, 370, 40);
    lv_textarea_set_one_line(add_num_ta, true); // 设置只能显示一行
    lv_obj_add_style(add_num_ta, &style, 0);    // 让数量输入使用中文字体样式
    lv_textarea_set_text(add_num_ta, "");
    lv_textarea_set_placeholder_text(add_num_ta, "请输入库存数"); // 给数量输入框设置默认提示信息

    add_price_ta = lv_textarea_create(add_page);
    lv_obj_set_size(add_price_ta, 240, 50);
    lv_obj_set_pos(add_price_ta, 120, 100);
    lv_textarea_set_one_line(add_price_ta, true); // 设置只能显示一行
    lv_obj_add_style(add_price_ta, &style, 0);    // 让价格输入使用中文字体样式
    lv_textarea_set_text(add_price_ta, "");
    lv_textarea_set_placeholder_text(add_price_ta, "请输入价格"); // 给价格输入框设置默认提示信息

    add_path_ta = lv_textarea_create(add_page);
    lv_obj_set_size(add_path_ta, 240, 50);
    lv_obj_set_pos(add_path_ta, 370, 100);
    lv_textarea_set_one_line(add_path_ta, true); // 设置只能显示一行
    lv_obj_add_style(add_path_ta, &style, 0);    // 让价格输入使用中文字体样式
    lv_textarea_set_text(add_path_ta, "");
    lv_textarea_set_placeholder_text(add_path_ta, "请输入路径"); // 给路径输入框设置默认提示信息

    // Button
    add_btn = lv_btn_create(add_page);
    lv_obj_set_size(add_btn, 100, 50);
    lv_obj_set_pos(add_btn, 650, 25);
    Label add_btn_lb = lv_label_create(add_btn);
    lv_obj_add_style(add_btn_lb, &style, 0);
    lv_label_set_text(add_btn_lb, "上架");
    lv_obj_add_event_cb(add_btn, add_good_click, LV_EVENT_CLICKED, NULL);

    // Keyboard
    // 创建拼音输入法
    pinyin_ime = lv_ime_pinyin_create(add_page);
    lv_obj_set_style_text_font(pinyin_ime, info.font, 0); // 使用已初始化的字体
    // 创建键盘并关联到拼音输入法
    add_kb = lv_keyboard_create(add_page);
    lv_obj_set_size(add_kb, 800, 220);
    lv_ime_pinyin_set_keyboard(pinyin_ime, add_kb);
    // 获取候选词面板并设置位置和大小
    cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
    lv_obj_set_size(cand_panel, LV_PCT(100), 30);
    lv_obj_align_to(cand_panel, add_kb, LV_ALIGN_OUT_TOP_MID, 0, 0);
    lv_obj_add_style(cand_panel, &style, 0);
    // 给文本区添加事件回调
    lv_obj_add_event_cb(add_name_ta, ta_event_cb, LV_EVENT_ALL, add_kb);
    lv_obj_add_event_cb(add_num_ta, ta_event_cb, LV_EVENT_ALL, add_kb);
    lv_obj_add_event_cb(add_price_ta, ta_event_cb, LV_EVENT_ALL, add_kb);
    lv_obj_add_event_cb(add_path_ta, ta_event_cb, LV_EVENT_ALL, add_kb);
    // 默认将键盘与商品名输入框关联
    lv_keyboard_set_textarea(add_kb, add_name_ta);
}

// 从上架界面返回
void add_to_manage(lv_event_t * e)
{
    lv_obj_del(add_page); // 关闭新增商品界面
    create_manage();
}

// 上架商品处理函数
void add_good_click(lv_event_t * e)
{
    const char * name      = lv_textarea_get_text(add_name_ta);
    const char * num_str   = lv_textarea_get_text(add_num_ta);
    const char * price_str = lv_textarea_get_text(add_price_ta);
    const char * path_str  = lv_textarea_get_text(add_path_ta);

    // 检查是否有空值
    if(strlen(name) == 0 || strlen(num_str) == 0 || strlen(price_str) == 0 || strlen(path_str) == 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "商品信息不能为空", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 判断商品是否已经存在
    if(query_node(head, name) != NULL) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "该商品已经存在", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 验证库存数量是否为整数
    char * endptr;
    long num = strtol(num_str, &endptr, 10);
    if(*endptr != '\0' || num <= 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "库存数量格式不正确", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 验证价格是否为浮点数
    float price = strtod(price_str, &endptr);
    if(*endptr != '\0' || price <= 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "价格格式不正确", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 插入商品到链表
    insert_tail(head, name, num, price);

    char path[128];
    // 验证路径对应的图片文件是否存在
    snprintf(path, sizeof(path), "%s%s%s", "/test_prj/imgs/", path_str, ".jpg");
    if(access(path, F_OK) != 0) {
        set_path(head, name, "S:/test_prj/imgs/empty.jpg");
        // 不存在但插入信息
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "商品上架成功\n但图片文件不存在", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
    } else {
        snprintf(path, sizeof(path), "%s%s%s", "S:/test_prj/imgs/", path_str, ".jpg");
        set_path(head, name, path);
        // 显示成功消息
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "成功", "商品上架成功", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
    }

    // 清空输入框
    lv_textarea_set_text(add_name_ta, "");
    lv_textarea_set_text(add_num_ta, "");
    lv_textarea_set_text(add_price_ta, "");
    lv_textarea_set_text(add_path_ta, "");
}

// 下架商品界面
void goto_del(lv_event_t * e)
{
    lv_obj_del(manage_page); // 把管理界面关闭

    // 创建下架界面
    del_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(del_page, 800, 480);

    // 添加返回按钮
    del_rt_btn = lv_btn_create(del_page);
    lv_obj_set_size(del_rt_btn, 100, 50);
    lv_obj_set_pos(del_rt_btn, 0, 25);
    del_rt_btn_lb = lv_label_create(del_rt_btn);
    lv_obj_add_style(del_rt_btn_lb, &style, 0);
    lv_label_set_text(del_rt_btn_lb, "返回");
    lv_obj_add_event_cb(del_rt_btn, del_to_manage, LV_EVENT_CLICKED, NULL);

    // 创建标题
    del_title_lb = lv_label_create(del_page);
    lv_obj_set_size(del_title_lb, 300, 80);          // 大小
    lv_obj_set_pos(del_title_lb, 200, 10);           // 坐标
    lv_obj_add_style(del_title_lb, &style, 0);       // 中文样式
    lv_label_set_text(del_title_lb, "下架商品界面"); // 添加文本

    // 商品名称输入框
    del_name_ta = lv_textarea_create(del_page);
    lv_obj_set_size(del_name_ta, 500, 50);
    lv_obj_set_pos(del_name_ta, 120, 50);
    lv_textarea_set_one_line(del_name_ta, true);
    lv_obj_add_style(del_name_ta, &style, 0);
    lv_textarea_set_text(del_name_ta, "");
    lv_textarea_set_placeholder_text(del_name_ta, "请输入要下架的商品名");

    // 下架按钮
    del_btn = lv_btn_create(del_page);
    lv_obj_set_size(del_btn, 100, 50);
    lv_obj_set_pos(del_btn, 650, 25);
    Label del_btn_lb = lv_label_create(del_btn);
    lv_obj_add_style(del_btn_lb, &style, 0);
    lv_label_set_text(del_btn_lb, "下架");
    lv_obj_add_event_cb(del_btn, delete_good_click, LV_EVENT_CLICKED, NULL);

    // 键盘设置
    pinyin_ime = lv_ime_pinyin_create(del_page);
    lv_obj_set_style_text_font(pinyin_ime, info.font, 0);
    del_kb = lv_keyboard_create(del_page);
    lv_obj_set_size(del_kb, 800, 300);
    lv_ime_pinyin_set_keyboard(pinyin_ime, del_kb);
    cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
    lv_obj_set_size(cand_panel, LV_PCT(100), 30);
    lv_obj_align_to(cand_panel, del_kb, LV_ALIGN_OUT_TOP_MID, 0, 0);
    lv_obj_add_style(cand_panel, &style, 0);
    lv_obj_add_event_cb(del_name_ta, ta_event_cb, LV_EVENT_ALL, del_kb);
    lv_keyboard_set_textarea(del_kb, del_name_ta);
}

// 删除商品处理函数实现
void delete_good_click(lv_event_t * e)
{
    const char * name = lv_textarea_get_text(del_name_ta);

    // 检查输入是否为空
    if(strlen(name) == 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "请输入商品名称", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 先查询商品是否存在
    struct good * found = query_node(head, name);
    if(found == NULL) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "没有找到该商品", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 执行删除操作
    int ret = delete_by_name(head, name);
    if(ret == 0) {
        // 删除成功
        char msg[64];
        snprintf(msg, sizeof(msg), "商品 %s 已成功下架", name);
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "成功", msg, NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        // 清空输入框
        lv_textarea_set_text(del_name_ta, "");
    } else {
        // 删除失败
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "失败", "商品下架失败", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
    }
}

// 从下架界面返回
void del_to_manage(lv_event_t * e)
{
    lv_obj_del(del_page); // 关闭删除商品界面
    create_manage();
}

// 修改商品界面
void goto_upd(lv_event_t * e)
{
    lv_obj_del(manage_page); // 把管理界面关闭

    // 创建修改界面
    upd_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(upd_page, 800, 480);

    // 添加返回按钮
    upd_rt_btn = lv_btn_create(upd_page);
    lv_obj_set_size(upd_rt_btn, 100, 50);
    lv_obj_set_pos(upd_rt_btn, 0, 25);
    upd_rt_btn_lb = lv_label_create(upd_rt_btn);
    lv_obj_add_style(upd_rt_btn_lb, &style, 0);
    lv_label_set_text(upd_rt_btn_lb, "返回");
    lv_obj_add_event_cb(upd_rt_btn, upd_to_manage, LV_EVENT_CLICKED, NULL);

    // 创建标题
    upd_title_lb = lv_label_create(upd_page);
    lv_obj_set_size(upd_title_lb, 300, 80);          // 大小
    lv_obj_set_pos(upd_title_lb, 200, 10);           // 坐标
    lv_obj_add_style(upd_title_lb, &style, 0);       // 中文样式
    lv_label_set_text(upd_title_lb, "修改商品界面"); // 添加文本

    // 商品名称输入框
    upd_name_ta = lv_textarea_create(upd_page);
    lv_obj_set_size(upd_name_ta, 500, 50);
    lv_obj_set_pos(upd_name_ta, 120, 40);
    lv_textarea_set_one_line(upd_name_ta, true);
    lv_obj_add_style(upd_name_ta, &style, 0);
    lv_textarea_set_placeholder_text(upd_name_ta, "请输入商品名称");

    // 查询按钮
    upd_query_btn = lv_btn_create(upd_page);
    lv_obj_set_size(upd_query_btn, 100, 50);
    lv_obj_set_pos(upd_query_btn, 650, 40);
    Label upd_query_lb = lv_label_create(upd_query_btn);
    lv_obj_add_style(upd_query_lb, &style, 0);
    lv_label_set_text(upd_query_lb, "查询");
    lv_obj_add_event_cb(upd_query_btn, query_and_fill_update, LV_EVENT_CLICKED, NULL);

    // 库存输入框
    upd_num_ta = lv_textarea_create(upd_page);
    lv_obj_set_size(upd_num_ta, 500, 50);
    lv_obj_set_pos(upd_num_ta, 120, 100);
    lv_textarea_set_one_line(upd_num_ta, true);
    lv_obj_add_style(upd_num_ta, &style, 0);
    lv_textarea_set_placeholder_text(upd_num_ta, "请输入新库存");

    // 价格输入框
    upd_price_ta = lv_textarea_create(upd_page);
    lv_obj_set_size(upd_price_ta, 500, 50);
    lv_obj_set_pos(upd_price_ta, 120, 160);
    lv_textarea_set_one_line(upd_price_ta, true);
    lv_obj_add_style(upd_price_ta, &style, 0);
    lv_textarea_set_placeholder_text(upd_price_ta, "请输入新价格");

    // 修改按钮
    upd_btn = lv_btn_create(upd_page);
    lv_obj_set_size(upd_btn, 100, 50);
    lv_obj_set_pos(upd_btn, 650, 100);
    Label upd_btn_lb = lv_label_create(upd_btn);
    lv_obj_add_style(upd_btn_lb, &style, 0);
    lv_label_set_text(upd_btn_lb, "修改");
    lv_obj_add_event_cb(upd_btn, update_good_click, LV_EVENT_CLICKED, NULL);

    // 键盘设置
    pinyin_ime = lv_ime_pinyin_create(upd_page);
    lv_obj_set_style_text_font(pinyin_ime, info.font, 0);
    upd_kb = lv_keyboard_create(upd_page);
    lv_obj_set_size(upd_kb, 800, 170);
    lv_ime_pinyin_set_keyboard(pinyin_ime, upd_kb);
    cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
    lv_obj_set_size(cand_panel, LV_PCT(100), 30);
    lv_obj_align_to(cand_panel, upd_kb, LV_ALIGN_OUT_TOP_MID, 0, 0);
    lv_obj_add_style(cand_panel, &style, 0);
    // 绑定输入框与键盘
    lv_obj_add_event_cb(upd_name_ta, ta_event_cb, LV_EVENT_ALL, upd_kb);
    lv_obj_add_event_cb(upd_num_ta, ta_event_cb, LV_EVENT_ALL, upd_kb);
    lv_obj_add_event_cb(upd_price_ta, ta_event_cb, LV_EVENT_ALL, upd_kb);
    lv_keyboard_set_textarea(upd_kb, upd_name_ta);
}

// 从修改界面返回
void upd_to_manage(lv_event_t * e)
{
    lv_obj_del(upd_page); // 关闭修改商品界面
    create_manage();
}

void query_and_fill_update(lv_event_t * e)
{
    const char * name = lv_textarea_get_text(upd_name_ta);

    if(strlen(name) == 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "请输入商品名称", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    struct good * found = query_node(head, name);
    if(found == NULL) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "未找到该商品", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);

        // 清空表单
        lv_textarea_set_text(upd_num_ta, "");
        lv_textarea_set_text(upd_price_ta, "");
    } else {
        // 填充表单
        char num_str[32], price_str[32];
        snprintf(num_str, sizeof(num_str), "%d", found->num);
        snprintf(price_str, sizeof(price_str), "%.2f", found->price);

        lv_textarea_set_text(upd_num_ta, num_str);
        lv_textarea_set_text(upd_price_ta, price_str);

        // 提示可以修改
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "请修改商品信息", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
    }
}

// 执行商品修改操作
void update_good_click(lv_event_t * e)
{
    const char * name      = lv_textarea_get_text(upd_name_ta);
    const char * num_str   = lv_textarea_get_text(upd_num_ta);
    const char * price_str = lv_textarea_get_text(upd_price_ta);

    // 检查输入完整性
    if(strlen(name) == 0 || strlen(num_str) == 0 || strlen(price_str) == 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "信息不能为空", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 验证数据格式
    char * endptr;
    long num = strtol(num_str, &endptr, 10);
    if(*endptr != '\0' || num <= 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "库存格式错误", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    double price = strtod(price_str, &endptr);
    if(*endptr != '\0' || price <= 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "价格格式错误", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 执行修改
    struct good * found = query_node(head, name);
    if(found != NULL) {
        found->num   = num;
        found->price = price;
        save_goods_to_file(head);

        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "成功", "商品信息已更新", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);

        // 清空表单
        lv_textarea_set_text(upd_name_ta, "");
        lv_textarea_set_text(upd_num_ta, "");
        lv_textarea_set_text(upd_price_ta, "");
    } else {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "商品不存在", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
    }
}

// 查询商品界面
void goto_qry(lv_event_t * e)
{
    lv_obj_del(manage_page); // 把管理界面关闭

    // 创建查询界面
    qry_page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(qry_page, 800, 480);

    // 添加返回按钮
    qry_rt_btn = lv_btn_create(qry_page);
    lv_obj_set_size(qry_rt_btn, 100, 50);
    lv_obj_set_pos(qry_rt_btn, 0, 25);
    qry_rt_btn_lb = lv_label_create(qry_rt_btn);
    lv_obj_add_style(qry_rt_btn_lb, &style, 0);
    lv_label_set_text(qry_rt_btn_lb, "返回");
    lv_obj_add_event_cb(qry_rt_btn, qry_to_manage, LV_EVENT_CLICKED, NULL);

    // 创建标题
    qry_title_lb = lv_label_create(qry_page);
    lv_obj_set_size(qry_title_lb, 300, 80);          // 大小
    lv_obj_set_pos(qry_title_lb, 200, 10);           // 坐标
    lv_obj_add_style(qry_title_lb, &style, 0);       // 中文样式
    lv_label_set_text(qry_title_lb, "查询商品界面"); // 添加文本

    // Textarea
    qry_name_ta = lv_textarea_create(qry_page);
    lv_obj_set_size(qry_name_ta, 500, 50);
    lv_obj_set_pos(qry_name_ta, 120, 50);
    lv_textarea_set_one_line(qry_name_ta, true); // 设置只能显示一行
    lv_obj_add_style(qry_name_ta, &style, 0);    // 让商品名输入使用中文字体样式
    lv_textarea_set_text(qry_name_ta, "");
    lv_textarea_set_placeholder_text(qry_name_ta, "请输入商品名"); // 给商品名输入框设置默认提示信息

    // Button
    qry_btn = lv_btn_create(qry_page);
    lv_obj_set_size(qry_btn, 100, 50);
    lv_obj_set_pos(qry_btn, 650, 25);
    Label qry_btn_lb = lv_label_create(qry_btn);
    lv_obj_add_style(qry_btn_lb, &style, 0);
    lv_label_set_text(qry_btn_lb, "查询");
    lv_obj_add_event_cb(qry_btn, query_good_click, LV_EVENT_CLICKED, NULL);

    // Keyboard
    // 创建拼音输入法
    pinyin_ime = lv_ime_pinyin_create(qry_page);
    lv_obj_set_style_text_font(pinyin_ime, info.font, 0); // 使用已初始化的字体
    // 创建键盘并关联到拼音输入法
    qry_kb = lv_keyboard_create(qry_page);
    lv_obj_set_size(qry_kb, 800, 300);
    lv_ime_pinyin_set_keyboard(pinyin_ime, qry_kb);
    // 获取候选词面板并设置位置和大小
    cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
    lv_obj_set_size(cand_panel, LV_PCT(100), 30);
    lv_obj_align_to(cand_panel, qry_kb, LV_ALIGN_OUT_TOP_MID, 0, 0);
    lv_obj_add_style(cand_panel, &style, 0);
    // 给文本区添加事件回调
    lv_obj_add_event_cb(qry_name_ta, ta_event_cb, LV_EVENT_ALL, qry_kb);
    // 默认将键盘与商品名输入框关联
    lv_keyboard_set_textarea(qry_kb, qry_name_ta);
}

// 从查询界面返回
void qry_to_manage(lv_event_t * e)
{
    lv_obj_del(qry_page); // 关闭查询商品界面
    create_manage();
}

// 查询商品处理函数
void query_good_click(lv_event_t * e)
{
    const char * name = lv_textarea_get_text(qry_name_ta);

    // 检查输入是否为空
    if(strlen(name) == 0) {
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "请输入商品名称", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
        return;
    }

    // 调用查询函数
    struct good * found = query_node(head, name);

    if(found != NULL) {
        // 找到商品，显示详细信息
        char msg[128];
        snprintf(msg, sizeof(msg), "商品名称: %s\n库存数量: %d\n商品价格: %.2f元", found->name, found->num,
                 found->price);
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "查询结果", msg, NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
    } else {
        // 未找到商品
        MessageBox msgbox = lv_msgbox_create(lv_scr_act(), "提示", "没有找到该商品", NULL, true);
        lv_obj_set_pos(msgbox, 280, 200);
        Label msg_text = lv_msgbox_get_text(msgbox);
        lv_obj_add_style(msg_text, &style, 0);
        Label msg_title = lv_msgbox_get_title(msgbox);
        lv_obj_add_style(msg_title, &style, 0);
    }
}